/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package sampledata.genomemap.data;

import cef.pui.pob.Builder;
import cef.pui.pob.BuilderException;
import commons.core.ValueTypes;
import commons.nb.data.DNData;
import commons.nb.data.DNDataList;
import genomemap.cef.pui.pob.Builders_Gen;
import genomemap.cef.pui.pob.OrganismDataBuilder;
import genomemap.data.KSA00Data;
import genomemap.data.OrganismData;
import genomemap.data.TAB08Data;

import java.util.Properties;

/**
 * @author Susanta Tewari
 * @version 1.0.0
 * @since 1.0.0
 */
public class DNData_DirBased_OrganismData extends DNData<OrganismData> {

    private final OrganismDataBuilder builder;
    private final String iconPath;

    /**
     * @param displayName display name
     * @param shortDescription short description
     * @param linkageGroups comma separated linkage groups
     * @param dirURL URL to the directory containing data files
     */
    public DNData_DirBased_OrganismData(final String displayName, final String shortDescription,
            final String linkageGroups, final String dirURL) {

        super("OrganismData", displayName, shortDescription);

        this.iconPath = "sampledata/genomemap/data/OrganismData16.png";

        final String ORGANISM_DATA_DIR_URL = "organismdata:files//" + dirURL;
        final Properties props             = new Properties();

        props.setProperty("genomemap.builder.OrganismData.linkage-groups", linkageGroups);
        props.setProperty("genomemap.builder.OrganismData.url", ORGANISM_DATA_DIR_URL);

        builder = Builders_Gen.of_Data_OrganismData_Dir(props, null);

        addDataList(create_DnDataList_Data_TAB08(ORGANISM_DATA_DIR_URL));
        addDataList(create_DnDataList_Data_KSA00(ORGANISM_DATA_DIR_URL));
        addDataList(create_DnDataList_StringList_Gene(ORGANISM_DATA_DIR_URL));
    }

    private DNDataList create_DnDataList_Data_KSA00(String ORGANISM_DATA_DIR_URL) {

        final DNDataList ksa00DNDataList = new DNDataList("N.crassa KSA00 Data");

        for (final Integer linkageGroup : builder.getLinkageGroups()) {

            final String label       = "KSA00 Data - " + linkageGroup;
            final String description = "KSA00 Data for linkage group " + linkageGroup;


            // set properties for the tab08 builder
            final Properties ksa00_builder_props = new Properties();
            final String prefix                  = "genomemap.builder.KSA00Data";

            ksa00_builder_props.setProperty(prefix + ".linkage-group", linkageGroup.toString());
            ksa00_builder_props.setProperty(prefix + ".url", ORGANISM_DATA_DIR_URL);
            ksa00_builder_props.setProperty(prefix + ".delegate",
                                            Builders_Gen.Data_KSA00_OrganismData.class.getName());

            final DNData<KSA00Data> dnData =
                DNData.create("KSA00Data", label, description,
                              "sampledata/genomemap/data/KSA00Data16.png",
                              cef.pui.pob.Builders.<KSA00Data>of_Delegate(prefix,
                                  ksa00_builder_props), KSA00Data.class);
            final Builder<KSA00Data> dnDataBuilder = dnData.getBuilder();

            dnDataBuilder.addPropertyKey("linkage-group");
            dnDataBuilder.addPropertyKey("url");
            ksa00DNDataList.addData(dnData);
        }

        return ksa00DNDataList;
    }

    private DNDataList create_DnDataList_Data_TAB08(final String ORGANISM_DATA_DIR_URL) {

        final DNDataList tab08DNDataList = new DNDataList("N.crassa TAB08 Data");

        for (final Integer linkageGroup : builder.getLinkageGroups()) {

            final String label       = "TAB08 Data - " + linkageGroup;
            final String description = "TAB08 Data for linkage group " + linkageGroup;


            // set properties for the tab08 builder
            final Properties tab08_builder_props = new Properties();
            final String prefix                  = "genomemap.builder.TAB08Data";

            tab08_builder_props.setProperty(prefix + ".linkage-group", linkageGroup.toString());
            tab08_builder_props.setProperty(prefix + ".url", ORGANISM_DATA_DIR_URL);
            tab08_builder_props.setProperty(prefix + ".delegate",
                                            Builders_Gen.Data_TAB08_OrganismData.class.getName());

            final DNData<TAB08Data> dnData =
                DNData.create("TAB08Data", label, description,
                              "sampledata/genomemap/data/TAB08Data16.png",
                              cef.pui.pob.Builders.<TAB08Data>of_Delegate(prefix,
                                  tab08_builder_props), TAB08Data.class);
            final Builder<TAB08Data> dnDataBuilder = dnData.getBuilder();

            dnDataBuilder.addPropertyKey("linkage-group");
            dnDataBuilder.addPropertyKey("url");
            tab08DNDataList.addData(dnData);
        }

        return tab08DNDataList;
    }

    private DNDataList create_DnDataList_StringList_Gene(final String ORGANISM_DATA_DIR_URL) {

        final DNDataList dnDataList = new DNDataList("N.crassa UGA Gene Lists");

        for (final Integer linkageGroup : builder.getLinkageGroups()) {

            final String label       = "ncrassa-UGA-GeneList - " + linkageGroup;
            final String description = "N. crassa UGA gene list for linkage group " + linkageGroup;


            // set properties for the tab08 builder
            final Properties stringlist_b_props = new Properties();
            final String prefix                 = "genomemap.builder.StringList-Gene.OrganismData";

            stringlist_b_props.setProperty(prefix + ".linkage-group", linkageGroup.toString());
            stringlist_b_props.setProperty(prefix + ".title", label);
            stringlist_b_props.setProperty(prefix + ".url", ORGANISM_DATA_DIR_URL);
            stringlist_b_props.setProperty(
                prefix + ".delegate", Builders_Gen.StringList_Gene_OrganismData.class.getName());

            final DNData<ValueTypes.StringList> dnData =
                DNData.create(label, label, description, null,
                              cef.pui.pob.Builders.<ValueTypes.StringList>of_Delegate(prefix,
                                  stringlist_b_props), ValueTypes.StringList.class);
            final Builder<ValueTypes.StringList> dnDataBuilder = dnData.getBuilder();

            dnDataBuilder.addPropertyKey("linkage-group");
            dnDataBuilder.addPropertyKey("url");
            dnDataBuilder.addPropertyKey("title");
            dnDataList.addData(dnData);
        }

        return dnDataList;
    }

    private DNDataList create_DnDataList_StringList_Probe(final String ORGANISM_DATA_DIR_URL) {

        final DNDataList probe_Stringlist = new DNDataList("N.crassa Probe List");

        for (final Integer linkageGroup : builder.getLinkageGroups()) {

            final String label       = "TAB08 Data - " + linkageGroup;
            final String description = "TAB08 Data for linkage group " + linkageGroup;


            // set properties for the tab08 builder
            final Properties tab08_builder_props = new Properties();
            final String prefix                  = "genomemap.builder.TAB08Data";

            tab08_builder_props.setProperty(prefix + ".linkage-group", linkageGroup.toString());
            tab08_builder_props.setProperty(prefix + ".url", ORGANISM_DATA_DIR_URL);
            tab08_builder_props.setProperty(
                prefix + ".delegate", Builders_Gen.StringList_Probe_OrganismData.class.getName());

            final DNData<TAB08Data> dnData =
                DNData.create("TAB08Data", label, description, null,
                              cef.pui.pob.Builders.<TAB08Data>of_Delegate(prefix,
                                  tab08_builder_props), TAB08Data.class);
            final Builder<TAB08Data> dnDataBuilder = dnData.getBuilder();

            dnDataBuilder.addPropertyKey("linkage-group");
            dnDataBuilder.addPropertyKey("url");
            probe_Stringlist.addData(dnData);
        }

        return probe_Stringlist;
    }

    @Override
    public String getIconPath() {
        return iconPath;
    }

    @Override
    public Builder<OrganismData> getBuilder() throws BuilderException {
        return builder;
    }

    @Override
    public Class<OrganismData> getBuilderValueType() {
        return OrganismData.class;
    }
}
